package com.example.jetpackdemo.basic_camera;

import android.content.Intent;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.jetpackdemo.PreviewActivity;
import com.example.jetpackdemo.R;
import com.example.jetpackdemo.view.RecordView;

import java.io.File;

/**
 * @author: tuyrt7 on 2020/12/2.
 */
public class FirstCameraActivity extends AppCompatActivity {
    private RecordView mRecordView;
    private FrameLayout mContainer;
    private CameraPreview mPreview;
    private boolean takingPicture;
    private String outputFilePath;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_camera);

        initView();
    }


    private void initView() {
        mContainer = findViewById(R.id.container);
        mRecordView = findViewById(R.id.record_view);
        mRecordView.setOnRecordListener(new RecordView.OnRecordListener() {
            @Override
            public void onTackPicture() {
                takingPicture = true;
                takePicture();
            }

            @Override
            public void onRecordVideo() {
                takingPicture = false;
                takeVideo();
            }

            @Override
            public void onFinish() {
                Log.i("aaaa", "===========onFinish: ");
                mPreview.stopRecording();
            }
        });

        startPreview();
    }


    private void startPreview() {
        mPreview = new CameraPreview(this);
        mContainer.addView(mPreview);

        mPreview.startCameraPreview();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mPreview == null) {
           startPreview();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        mPreview = null;
    }

    private void takePicture() {
        String dirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/xiaoke/pic";
        File dir = new File(dirPath);
        if (!dir.exists() ||!dir.isDirectory()) {
            dir.mkdirs();
        }
        File file = new File(dirPath, System.currentTimeMillis() + ".jpg");
        Log.d("aaaa", "takePicture: ");
        mPreview.takePicture(file, new CameraPreview.OnImageSavedCallback() {
            @Override
            public void onImageSaved(@NonNull Uri imageUri) {
                Log.d("aaaa", "stop takePicture: ");
                outputFilePath = file.getAbsolutePath();
                onFileSaved(imageUri);
            }

            @Override
            public void onError(@NonNull String exception) {
                Log.e("aaaa", "onError: " + exception);
            }
        });
    }

    private void takeVideo() {
        String dirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/xiaoke/video";
        File dir = new File(dirPath);
        if (!dir.exists() ||!dir.isDirectory()) {
            dir.mkdirs();
        }
        File file = new File(dirPath, System.currentTimeMillis() + ".mp4");
        Log.d("aaaa", "start takeVideo: ");
        mPreview.startRecording(file, new CameraPreview.OnVideoSavedCallback() {
            @Override
            public void onVideoSaved(@NonNull Uri videoUri) {
                Log.d("aaaa", "stop takeVideo: ");
                outputFilePath = file.getAbsolutePath();
                onFileSaved(videoUri);
            }

            @Override
            public void onError(@NonNull String exception) {
                Log.e("aaaa", "onError: " + exception);
            }
        });
    }

    private void onFileSaved(Uri savedUri) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            this.sendBroadcast(new Intent(android.hardware.Camera.ACTION_NEW_PICTURE, savedUri));
        }
        String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(savedUri.getPath()));
        Log.i("aaaa","onFileSaved mimeType:" + mimeType);
        //扫描到相册
        MediaScannerConnection.scanFile(this, new String[]{new File(savedUri.getPath()).getAbsolutePath()},
                new String[]{mimeType}, null);

        Log.i("aaaa","onFileSaved outPath:" + outputFilePath);
        //预览
        PreviewActivity.start(this, outputFilePath, !takingPicture);
    }
}
